#include "stdafx.h"
#include "wf_modifystretch.h"

#include "pf_snapper.h"



WF_ModifyStretch::WF_ModifyStretch(PF_Container& container,
        PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Stretch Entities",
                           container, graphicView) {

    firstCorner = PF_Vector(false);
    secondCorner = PF_Vector(false);
    referencePoint = PF_Vector(false);
    targetPoint = PF_Vector(false);
}

QAction* WF_ModifyStretch::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	// tr("Stretch")
    QAction* action = new QAction(tr("&Stretch"), NULL);
	action->setIcon(QIcon(":/extui/modifystretch.png"));
    //action->zetStatusTip(tr("Stretch Entities"));
    return action;
}


void WF_ModifyStretch::init(int status) {
    PF_ActionInterface::init(status);

}



void WF_ModifyStretch::trigger() {

    PF_DEBUG->print("PF_ActionModifyStretch::trigger()");

    deletePreview();

    PF_Modification m(*container, graphicView);
    m.stretch(firstCorner, secondCorner, targetPoint-referencePoint);

    setStatus(SetFirstCorner);

    PF_DIALOGFACTORY->updateSelectionWidget(container->countSelected());
}



void WF_ModifyStretch::mouseMoveEvent(PF_MouseEvent* e) {
    PF_DEBUG->print("PF_ActionModifyStretch::mouseMoveEvent begin");

    PF_Vector mouse = snapPoint(e);
    switch (getStatus()) {
    case SetFirstCorner:
        break;

    case SetSecondCorner:
        if (firstCorner.valid) {
            secondCorner = snapPoint(e);
            deletePreview();
            preview->addEntity(
                new PF_Line(preview,
                            PF_LineAttribute(PF_Vector(firstCorner.x,
                                                  firstCorner.y),
                                        PF_Vector(secondCorner.x,
                                                  firstCorner.y))));
            preview->addEntity(
                new PF_Line(preview,
                            PF_LineAttribute(PF_Vector(secondCorner.x,
                                                  firstCorner.y),
                                        PF_Vector(secondCorner.x,
                                                  secondCorner.y))));
            preview->addEntity(
                new PF_Line(preview,
                            PF_LineAttribute(PF_Vector(secondCorner.x,
                                                  secondCorner.y),
                                        PF_Vector(firstCorner.x,
                                                  secondCorner.y))));
            preview->addEntity(
                new PF_Line(preview,
                            PF_LineAttribute(PF_Vector(firstCorner.x,
                                                  secondCorner.y),
                                        PF_Vector(firstCorner.x,
                                                  firstCorner.y))));
            drawPreview();
        }
        break;

    case SetReferencePoint:
        break;

    case SetTargetPoint:
        if (referencePoint.valid) {
            targetPoint = mouse;

            deletePreview();
            preview->addStretchablesFrom(*container, firstCorner, secondCorner);
            //preview->move(targetPoint-referencePoint);
            preview->stretch(firstCorner, secondCorner,
                             targetPoint-referencePoint);
            drawPreview();
        }
        break;

    default:
        break;
    }

    PF_DEBUG->print("PF_ActionModifyStretch::mouseMoveEvent end");
}



void WF_ModifyStretch::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        PF_CoordinateEvent ce(snapPoint(e));
        coordinateEvent(&ce);
    } else if (e->button()==Qt::RightButton) {
        deletePreview();
        init(getStatus()-1);
    }
}



void WF_ModifyStretch::coordinateEvent(PF_CoordinateEvent* e) {
    if (e==NULL) {
        return;
    }

    PF_Vector mouse = e->getCoordinate();

    switch (getStatus()) {
    case SetFirstCorner:
        firstCorner = mouse;
        setStatus(SetSecondCorner);
        break;

    case SetSecondCorner:
        secondCorner = mouse;
        deletePreview();
        setStatus(SetReferencePoint);
        break;

    case SetReferencePoint:
        referencePoint = mouse;
        graphicView->moveRelativeZero(referencePoint);
        setStatus(SetTargetPoint);
        break;

    case SetTargetPoint:
        targetPoint = mouse;
        graphicView->moveRelativeZero(targetPoint);
        trigger();
        //finish();
        break;

    default:
        break;
    }

}


void WF_ModifyStretch::updateMouseButtonHints() {
    switch (getStatus()) {
    case SetFirstCorner:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Specify first corner"),
                                            tr("Cancel"));
        break;
    case SetSecondCorner:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Specify second corner"),
                                            tr("Back"));
        break;
    case SetReferencePoint:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Specify reference point"),
                                            tr("Back"));
        break;
    case SetTargetPoint:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Specify target point"),
                                            tr("Back"));
        break;
    default:
        PF_DIALOGFACTORY->updateMouseWidget("", "");
        break;
    }
}



void WF_ModifyStretch::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_ModifyStretch::updateToolBar() {
    if (!isFinished()) {
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
    } else {
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarModify);
    }
}


// EOF
